home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Misc / Wood.0.72 / Sources / InfoView.m < prev    next >
Encoding:
Text File  |  1995-09-10  |  7.5 KB  |  347 lines

  1.  
  2. #import "InfoView.h"
  3.  
  4. #import <math.h>
  5.  
  6. #define RANDINT(n) (random() % ((n)+1))         
  7.  
  8. inline float restrictValue(float val, float max, float min)
  9. {
  10.     if (val > max)
  11.         return max;
  12.     else if (val < min)
  13.         return min;
  14.     else
  15.         return val;
  16. }
  17.  
  18. @implementation InfoView
  19.  
  20. void handler(DPSTimedEntry teNumber, double now, void *userData)
  21. {
  22.     id obj = (id) userData;
  23.     [obj animationClick];
  24. }
  25.  
  26. - initFrame:(NXRect *)frameRect;
  27. {
  28.        id bundle;
  29.     char buf[MAXPATHLEN + 1];
  30.     char fileName[MAXPATHLEN + 1];
  31.  
  32.       [super initFrame:frameRect];
  33.     bundle = [NXBundle bundleForClass:[self class]];
  34.     if(![bundle getPath:buf 
  35.         forResource:"InfoPanel" 
  36.         ofType:"nib"])
  37.         return nil;
  38.     strcpy(fileName, buf);
  39.     strcat(fileName, "/OutOfTheWood.tiff");    
  40.        oldImage = frontImage = [[NXImage alloc] initFromFile:fileName];
  41.     strcpy(fileName, buf);
  42.     strcat(fileName, "/Credits.tiff");
  43.     image = [[NXImage alloc] initFromFile:fileName];
  44.     strcpy(fileName, buf);
  45.     strcat(fileName, "/bat.tiff");
  46.        bat = [[NXImage alloc] initFromFile:fileName];
  47.     strcpy(fileName, buf);
  48.     strcat(fileName, "/BatSound.snd");
  49.        batSound = [[Sound alloc] initFromSoundfile:fileName];
  50.     toggle = YES;
  51.        [image getSize:&imageSize];
  52.     [bat getSize:&batSize];
  53.        buffer = [[NXImage alloc] initSize:&imageSize];      
  54.     [self compositeWithDissolve:NO andDelta:0.0];
  55.     frontPage = YES;
  56.        return self;
  57. }
  58.    
  59. - free;
  60. {
  61.     [self removeTe];
  62.        [image free];
  63.        [oldImage free];
  64.        [buffer free];
  65.     [bat free]; 
  66.        return [super free];
  67. }
  68.  
  69. - compositeWithDissolve:(BOOL)aBool andDelta:(float)aDelta
  70. {
  71.     NXPoint pt = {0.0,0.0};
  72.  
  73.        [buffer lockFocus];
  74.       [oldImage composite:NX_COPY toPoint:&pt];
  75.        if(aBool)
  76.            [image dissolve:aDelta toPoint:&pt];    
  77.        [buffer unlockFocus];    
  78.        return self;
  79. }
  80.  
  81. - compositeWithChecker:(BOOL)aBool andDelta:(float)aDelta
  82. {
  83.     NXRect rects[64], r;
  84.     NXSize imgSize,rectSize;
  85.     NXPoint pt = {0.0,0.0};
  86.     float height,h;
  87.     int x, y, index;
  88.     id img;
  89.     
  90.     [buffer lockFocus];
  91.     [oldImage composite:NX_COPY toPoint:&pt];
  92.     if(!aBool){
  93.         [buffer unlockFocus];
  94.         return self;
  95.     }
  96.     [image getSize:&imgSize];
  97.     img = [[NXImage alloc] initSize:&imgSize];
  98.     rectSize.width = imgSize.width / 8;
  99.     rectSize.height = imgSize.height / 8;
  100.     height = 2 * aDelta * rectSize.height;
  101.     h = height - rectSize.height;
  102.     for(x = 0; x < 8; x ++){
  103.         for(y = 0; y < 8; y ++){
  104.             index = x + y * 8;
  105.             if(height > rectSize.height){
  106.                 if((x % 2) == (y % 2)){
  107.                     NXSetRect(&rects[index],x * rectSize.width,
  108.                     (y + 1) * rectSize.height - h,
  109.                     rectSize.width, h);
  110.                 } else {
  111.                     NXSetRect(&rects[index],x * rectSize.width,
  112.                     y * rectSize.height,
  113.                     rectSize.width,rectSize.height);
  114.                 }
  115.             } else {
  116.                 if((x % 2) != (y % 2)){
  117.                     NXSetRect(&rects[index],x * rectSize.width,
  118.                     y * rectSize.height,rectSize.width, height);
  119.                 } else {
  120.                        NXSetRect(&rects[index],0,0,0,0);
  121.                 }
  122.             }
  123.         }
  124.     }
  125.     [img lockFocus];
  126.     PSsetgray(0.0);
  127.     PSsetalpha(0.0);
  128.     NXSetRect(&r,0,0,imgSize.width,imgSize.height);
  129.     NXRectFill(&r);
  130.     PSsetalpha(1.0);
  131.     NXRectFillList(rects,64);
  132.     [image composite:NX_SIN toPoint:&pt];
  133.     [img unlockFocus];
  134.     [img composite:NX_SOVER toPoint:&pt];
  135.     [buffer unlockFocus];
  136.     [img free];
  137.     return self;
  138. }
  139.  
  140. - drawSelf:(NXRect *)rects :(int)rectCount;
  141. {
  142.    NXPoint p1 = {0.0, 0.0};
  143.     
  144.    PSsetgray (NX_LTGRAY);
  145.    NXRectFill (&bounds);
  146.    [buffer composite:NX_COPY toPoint:&p1];
  147.    return self;
  148. }
  149.  
  150. - mouseDown:(NXEvent *) thisEvent
  151. {
  152.        delta = 0.0;        
  153.        [self compositeWithDissolve:NO andDelta:delta];
  154.     [self display];
  155.     animationDissolve =YES;
  156.     animationCrowl = NO;
  157.     animationStep = 0; 
  158.     if(!animateTe)
  159.         animateTe = DPSAddTimedEntry(.03,(DPSTimedEntryProc) handler, self, NX_BASETHRESHOLD);   
  160.     return self;
  161. }
  162.  
  163. - start:sender
  164. {
  165.     [self display];
  166.     [self removeTe];
  167.     animationDissolve = NO;
  168.     animationCrowl = YES;
  169.     animationStep = 0;
  170.     randDirection = 0;
  171.     batRect.origin.y = 50.0;
  172.     batRect.origin.x = 50.0;
  173.     batRect.size.width = batSize.width;
  174.     batRect.size.height = batSize.height / 4;
  175.     if(!animateTe)
  176.         animateTe = DPSAddTimedEntry(.03,(DPSTimedEntryProc) handler, self, NX_BASETHRESHOLD);    
  177.     return self;
  178. }
  179.  
  180. - stop:sender
  181. {
  182.     [self removeTe];
  183.     return self;
  184. }
  185.  
  186. - animationClick
  187. {
  188.     if(animationCrowl)
  189.         [self animateCrowl];
  190.     if(animationDissolve)
  191.         [self animateDissolve];
  192.     NXPing();
  193.     return self;
  194. }
  195.  
  196. - animateCrowl
  197. {
  198.     NXRect tmpRect;
  199.     int nr;
  200.  
  201.     [self lockFocus];
  202.         [self drawBackground:&batRect];
  203.         if(animationStep % 11 == 0)
  204.             randDirection = RANDINT(7);
  205.         if(animationStep % 13 == 0 && randDirection % 2 == 0)
  206.             [batSound play:self];
  207.         switch(randDirection){
  208.             case 0 :batRect.origin.x += 1;
  209.                     break;
  210.             case 1 :batRect.origin.x += 1;
  211.                     batRect.origin.y += 1;
  212.                     break;
  213.             case 2 :batRect.origin.y += 1;
  214.                     break;
  215.             case 3 :batRect.origin.x -= 1;
  216.                     batRect.origin.y += 1;
  217.                     break;
  218.             case 4 :batRect.origin.x -= 1;
  219.                     break;
  220.             case 5 :batRect.origin.x -= 1;
  221.                     batRect.origin.y -= 1;
  222.                     break;
  223.             case 6 :batRect.origin.y -= 1;
  224.                     break;
  225.             case 7 :batRect.origin.x += 1;
  226.                     batRect.origin.y -= 1;
  227.                     break;
  228.         }    
  229.         animationStep++;
  230.         batRect.origin.x = restrictValue(batRect.origin.x,200,50);
  231.         batRect.origin.y = restrictValue(batRect.origin.y,230,50);
  232.         NXSetRect(&tmpRect,0,0,batSize.width,batSize.height / 4);
  233.         nr = animationStep % 8;
  234.         switch(nr){
  235.             case 1:
  236.             case 6:
  237.                     tmpRect.origin.y += batSize.height / 4;
  238.                     break;
  239.             case 2:
  240.             case 5:
  241.                     tmpRect.origin.y += batSize.height / 2;
  242.                     break;
  243.             case 3:
  244.             case 4:
  245.                     tmpRect.origin.y += 3 * batSize.height / 4;
  246.                     break;
  247.         }
  248.         [bat composite:NX_SOVER fromRect:&tmpRect toPoint:&batRect.origin];
  249.         [[self window] flushWindow];
  250.     [self unlockFocus];
  251.     return self;
  252. }
  253.  
  254. - animateDissolve
  255. {
  256.     id dummy;
  257.  
  258.     delta = delta + 0.20;
  259.     if(toggle)
  260.         [self compositeWithDissolve:YES andDelta:delta];
  261.     else
  262.         [self compositeWithChecker:YES andDelta:delta];
  263.     [self display];
  264.     if(delta < 1.0)
  265.         return self;
  266.     toggle = !toggle;
  267.     dummy = oldImage;
  268.     oldImage = image;
  269.     image = dummy;
  270.     frontPage = (!frontPage);
  271.     [self removeTe];
  272.     if(frontPage)
  273.         [self start:self];
  274.     return self;
  275. }
  276.  
  277. - removeTe
  278. {
  279.     if(animateTe){
  280.         DPSRemoveTimedEntry(animateTe);
  281.         animateTe = 0;
  282.     }
  283.     return self;
  284. }
  285.  
  286. - drawBackground:(NXRect *)rect
  287. {
  288.     NXRect tmpRect = *rect;
  289.  
  290.     NX_X(&tmpRect) = floor(NX_X(&tmpRect));
  291.     NX_Y(&tmpRect) = floor(NX_Y(&tmpRect));
  292.     if (NXDrawingStatus == NX_DRAWING) {
  293.         PSsetgray (NX_WHITE);
  294.         PScompositerect (NX_X(&tmpRect), NX_Y(&tmpRect),
  295.                           NX_WIDTH(&tmpRect), NX_HEIGHT(&tmpRect), NX_SOVER);
  296.     }
  297.     [frontImage composite:NX_SOVER fromRect:&tmpRect toPoint:&tmpRect.origin];
  298.     return self;
  299. }
  300.  
  301. - windowWillClose:sender
  302. {
  303.     id dummy;
  304.     
  305.     [self stop:self];
  306.     if(!frontPage){
  307.         toggle = !toggle;
  308.         frontPage = YES;
  309.         dummy = oldImage;
  310.         oldImage = image;
  311.         image = dummy;
  312.         [self compositeWithDissolve:NO andDelta:0.0];
  313.     }
  314.     return self;
  315. }
  316.  
  317. - woodPage:sender
  318. {
  319.     Pasteboard *pboard = nil;
  320.     static const char *url = NULL;
  321.     const char *types[1];
  322.     
  323.     if(!url){
  324.         if(strings)
  325.             url = [strings valueForStringKey:"URL"];
  326.         else
  327.             url= NXUniqueString("file:/Hoffmann/Uwe/Library/HTML/HomePage/Wood/Wood.html");
  328.     }
  329.     if(!pboard)
  330.         pboard = [Pasteboard newUnique];
  331.     types[0] = NXAsciiPboardType;
  332.     [pboard declareTypes:types num:1 owner:NXApp];
  333.     [pboard writeType:NXAsciiPboardType 
  334.         data:url length:strlen(url)];
  335.     if(NXPerformService("OmniWeb/Open URL", pboard))
  336.         [window performClose:self];
  337.     else if(NXPerformService("SpiderWoman/Open URL", pboard))
  338.         [window performClose:self];
  339.     else if(NXPerformService("NetSurfer/Open URL", pboard))
  340.         [window performClose:self];
  341.     else
  342.         NXBeep();
  343.     return self;
  344. }
  345.  
  346. @end
  347.